<?php

namespace app\api\model;

use app\common\model\User;
use extend\Poster as PosterExtend;
use think\Model;
require_once ROOT_PATH.'extend/Poster.php';

class Poster extends Model
{




    //海报
    public function poster($page,$qrcode_param,$app_type)
    {
        $qrcode_info = $this->getQrcode($app_type, $page, $qrcode_param);

        if ($qrcode_info[ 'code' ] < 0) throw new \Exception($qrcode_info['msg']);

        $member_info = User::get($qrcode_param[ 'suid' ]);
        if (empty($member_info))throw new \Exception('未获取到会员信息');

        try {

            $poster = new PosterExtend(740, 1100);
            $option = [
                [
                    'action' => 'setBackground', // 背景色
                    'data' => [255, 255, 255]
                ],
                [
                    'action' => 'imageCopy', //
                    'data' => [
                        ROOT_PATH.'public/assets/img/poster_bg.png',
                        0,
                        0,
                        750,
                        1132,
                        0
                    ]
                ],
                [
                    'action' => 'imageCopy', // 写入二维码
                    'data' => [
                        $qrcode_info[ 'data' ][ 'path' ],
                        300,
                        850,
                        150,
                        150
                    ]
                ],

            ];

            $option_res = $poster->create($option);
            if (is_array($option_res)) return $option_res;

            $res = $option_res->jpeg('upload/poster/user', 'user' . $qrcode_param[ 'suid' ] . '_' . $app_type);

            if ($res['code']==0){
                return $res['data']['path'];
            } else {
                throw new \Exception($res['message']);
            }
            //return $res;

        }  catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 获取二维码
     * @param unknown $app_type 请求类型
     * @param unknown $page uniapp页面路径
     * @param unknown $qrcode_param 二维码携带参数
     * @param string $promotion_type 活动类型 null为无活动
     */
    private function getQrcode($app_type, $page, $qrcode_param)
    {
        $data = [
            'app_type' => $app_type,
            'type' => 'get',
            'data' => $qrcode_param,
            'page' => $page,
            'qrcode_path' => 'upload/qrcode/user_share',
            'qrcode_name' => 'user_share' . '_' . $qrcode_param[ 'suid' ],
        ];
        $qrcode_model = new Qrcode();

        $res = $qrcode_model->createQrcode($data);

        return $res;
    }

}
